package com.xindz.hgq.base;

import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.kingja.loadsir.core.LoadSir;
import com.xindz.hgq.R;

/**
 * @name HGQBase
 * @class name：com.xindz.hgq.title
 * @describe Toolbar 的Menu的弹出位置覆盖ToolBar的问题通过  app:popupTheme="@style/ToolbarPopupTheme" 设置不覆盖锚点解决
 * Toolbar menu 的三个小点的颜色通过 Theme   <item name="android:textColorSecondary">#ffffff</item><item name="android:actionMenuTextColor">#ffffff</item> 解决
 * @anthor XINDZ
 * @time 2018/1/11 14:01
 * @change
 * @chang time
 * @class describe
 */
public abstract class BaseTitleToolBarActivity<V extends BaseView, P extends BasePresenter<V>> extends BaseActivity<V, P> {
    private View rootView;
    private CoordinatorLayout cdlBase;
    private AppBarLayout ablBase;
    private Toolbar tbBaseTitle;
    private TextView tvBaseTitle;
    private FrameLayout flBaseContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    /**
     * 重写了setContentView 方法
     * 如果Options中开启了OpenTitle
     * 则用AddView的方法添加toolbar
     * 否则使用super.setContentView(layoutResID);
     *
     * @param layoutResID
     */
    @Override
    public void setContentView(int layoutResID) {
        if (getOptions().isOpenTitle()) {
            rootView = View.inflate(this, R.layout.layout_base_activity_toolbar, null);

            cdlBase = rootView.findViewById(R.id.cdl_base);

            ablBase = rootView.findViewById(R.id.abl_base);

            tbBaseTitle = rootView.findViewById(R.id.tb_base_title);

            tvBaseTitle = rootView.findViewById(R.id.tv_base_title);

            flBaseContent = rootView.findViewById(R.id.fl_base_content);

            //设置Toolbar 自带title不显示 方法1
//            tbBaseTitle.setTitle("");

            setSupportActionBar(tbBaseTitle);
            //设置Toolbar 自带title不显示 方法2
            getSupportActionBar().setDisplayShowTitleEnabled(false);

            View content = View.inflate(this, layoutResID, null);
            if (content != null) {
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
                flBaseContent.addView(content, layoutParams);
                //如果开启LaodSir
                if (getOptions().isOpenLoadSir()) {
                    loadService = LoadSir.getDefault().register(content, getOptions().getLoadSirListener());
                }
            }

            setContentView(rootView);

        } else {
            super.setContentView(layoutResID);
        }
    }

    /**
     * 设置标题 默认是APP NAME
     *
     * @param title
     */
    public void showTitle(@NonNull String title) {
        if (getOptions().isOpenTitle() && tvBaseTitle != null) {
            tvBaseTitle.setText(title);
        }
    }

    /**
     * 设置Menu 的点击事件
     *
     * @param listener
     */
    public void setMenuOnClickListerner(@NonNull Toolbar.OnMenuItemClickListener listener) {
        if (getOptions().isOpenTitle() && tbBaseTitle != null) {
            tbBaseTitle.setOnMenuItemClickListener(listener);
        }
    }


    /**
     * 设置Toolbar 划出
     */
    public void setToolBarScroll() {
        if (getOptions().isOpenTitle() && tbBaseTitle != null) {
            AppBarLayout.LayoutParams appBarLayoutParams = (AppBarLayout.LayoutParams) tbBaseTitle.getLayoutParams();
            appBarLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
            tbBaseTitle.setLayoutParams(appBarLayoutParams);
        }
    }

    /**
     * 设置TOOLBAR 左侧返回键 点击返回
     */
    public void showLeftBack() {
        showLeftButton(R.drawable.ic_back_toleft, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 设置TOOLBAR 左侧ICON 和点击事件
     *
     * @param iconId
     * @param listener
     */
    public void showLeftButton(@DrawableRes int iconId, View.OnClickListener listener) {
        if (getOptions().isOpenTitle() && tbBaseTitle != null) {
            //设置左侧按钮ICON
            tbBaseTitle.setNavigationIcon(iconId);
            //设置左侧按钮点击事件
            tbBaseTitle.setNavigationOnClickListener(listener);
        }
    }

}
